<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html>
 <head>
    <title>销售冻结批次管理</title>
    <!-- import css and js -->
	<%@ include file="/common/meta.jsp" %>
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/ligerUI/js/plugins/ligerLayout.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctx}/js/jsp/yd/logic/ItemStateData.js"></script>
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script type="text/javascript">
		var basic_Url="${ctx}/yd/logic/sowarefreezedeal";
		var ItemStateList = ItemStateData.Rows;
		var item = eval("("+'${sItemBatchDO}'+")");
		var batch = '${itemBatch}';
		var whcode = '${whcode}';
		var grid;
		$(function(){
			var column1 =  [
			{ display: 'ID', name: 'id', width: 1 , hide:true},
			{ display: '产品id', name: 'materialsid', width: 1 , hide:true},//使用selecturl时必须设置该属性
			{ display: '<font color="gray">产品编码</font>', name: 'prodNo', width: 100},
			{ display: '<font color="gray">产品名称</font>', name: 'prodName', width: 80 },
			{ display: '<font color="gray">型号规格</font>', name: 'prodSpecDesc', width: 120 },
			{ display: '<font color="gray">基本单位</font>', name: 'prodBUnit', width: 80},
			{ display: '<font color="gray">单位换算率</font>', name: 'prodUnitConvert', width: 70},
			{ display: '<font color="gray">单位</font>', name: 'prodUnit', width: 70 },
			{ display : '产品批次<font color="red">*</font>',name : 'prodBatch',width : 80, isSort: false
				  ,editor: { /*onChanged:getkcQuantity,*/type: 'selectbatch',whcode:'whcode', databatch:'batchItem',path:'${ctx}/yd/logic/otherOutwh/getWhBatch.do', valueColumnName: 'batch', displayColumnName: 'batch',
			 			ext:function (rowdata)
			          {
			              return {
			                  onBeforeOpen: function(){
			                  	if(whcode==null || ""==whcode){
			              			$.dialog.alert("请先选择仓库");
			              			return ;
			              		}
			                  	getDG().dialog({
							    		title:'选择批次',
							    		width : 300,
										height : 350,
										content: 'url:' + sys_context+'/public/windowalert/itembatch.do?whCode='+whcode+'&maCode='+rowdata.prodNo,//
							    		lock : true,
							    		max : true,
							    		min : true,
							    		ok: function(){
							    			var rows = this.content.getSelected();
											if (rows != null && undefined != rows && ""!=rows) {
												grid.updateCell('prodBatch', rows[0].batch, rowdata);
												grid.endEdit();
												getBatchkcQuantity(rowdata);
											}
											this.content.Close();
							        		return false;
							    	    },
							    	    cancelVal: '关闭',
							    	    cancel: function(){
							    	    	grid.endEdit();
							    	    } 
							    	}).zindex();
			                  }, render: function ()
			                  { 
			                  	var batchItem = $.parseJSON($("#batchItem").val());
			      		  		if(batchItem!=null){
			      			 	for (var i = 0; i < batchItem.length; i++)
			               			{
			                   			if (batchItem[i]['batch'] == rowdata.prodBatch){
			                      	 		return batchItem[i]['batch'];
			                   			} 
			               			}
			      		  		}
			                  }
			
			              };
			          }
			 		},
			 		render: function (item){
					 		var batchItem = $.parseJSON($("#batchItem").val());
				  		if(undefined != batchItem && "" != batchItem){
					 	for (var i = 0; i < batchItem.length; i++)
			   			{
			       			if (batchItem[i]['batch'] == item.prodBatch){
			          	 		return batchItem[i]['batch'];
			       			} 
			   			}
				  		}
			          return item.prodBatch;
			      }
				}, 
				{ display: '<font color="gray">等级</font>', name: 'status', width: 60
					,render: function (item){
			  		  var state=item.status;
			  		  if(state=="1"){
			  			  return "一等品";
			  		  }
			  		  if(state=="2"){
			  			  return "次品";
			  		  }
			  		  if(state=="3"){
			  			  return "旧件";
			  		  }
			  	  	}
				},
				{display : '已出库基本单位数量',name : 'ckBQuantity',width : 1,hide : true
				},{display : '<font color="gray">订单已出库数量</font>',name : 'ckQuantity',width : 100},
				{display : '可用库存基本单位数量</font>',name : 'kyBQuantity',width : 1 , hide:true},
				{display : '<font color="gray">可用库存数量</font>',name : 'kyQuantity',width : 100},
				{ display: '解冻基本单位数量', name: 'djjdBQuantity', width: 1 , hide:true},
                { display: '<font color="gray">本单解冻数量</font>', name: 'djjdQuantity', width: 90,
              	  render:function(item){
    					if(undefined == item.djjdQuantity || null == item.djjdQuantity || isNaN(item.djjdQuantity) ){
    			    		item.djjdQuantity = 0;
    			    		item.djjdBQuantity = 0;
    			    	}
    					
    				  return item.djjdQuantity+"";
    			  	}
                },
				{display : '<font color="gray">本单可冻结数量</font>',name : 'tmpCanFrozenQuantity',width : 100
				},
				{ display: '<font color="gray">实际冻结基本单位数量</font>', name: 'djBQuantity', width:1 , hide:true},
			    { display: '实际冻结数量<font color="red">*</font>', name: 'djQuantity', width: 80
			  	  ,editor: { type: 'float'},
			  	  render:function(item){
							if(!isNaN(item.djQuantity) && !isNaN(item.prodUnitConvert)){
					    		item.djBQuantity = item.djQuantity*item.prodUnitConvert ;
					    	}else{
					    		item.djBQuantity = null;//清除值
					    	}
							
						  return item.djQuantity;
					  	}
			    }
			];
			
			
			var column2 =  [
			{ display: 'ID', name: 'id', width: 1 , hide:true},
			{ display: '产品id', name: 'materialsid', width: 1 , hide:true},//使用selecturl时必须设置该属性
			{ display: '<font color="gray">产品编码</font>', name: 'prodNo', width: 100},
			{ display: '<font color="gray">产品名称</font>', name: 'prodName', width: 80 },
			{ display: '<font color="gray">型号规格</font>', name: 'prodSpecDesc', width: 120 },
			{ display: '<font color="gray">基本单位</font>', name: 'prodBUnit', width: 80},
			{ display: '<font color="gray">单位换算率</font>', name: 'prodUnitConvert', width: 70},
			{ display: '<font color="gray">单位</font>', name: 'prodUnit', width: 70 },
			{ display : '产品批次<font color="red">*</font>',name : 'prodBatch',width : 80}, 
				{ display: '<font color="gray">等级</font>', name: 'status', width: 60
					,render: function (item){
			  		  var state=item.status;
			  		  if(state=="1"){
			  			  return "一等品";
			  		  }
			  		  if(state=="2"){
			  			  return "次品";
			  		  }
			  		  if(state=="3"){
			  			  return "旧件";
			  		  }
			  	  	}
				},
				{display : '已出库基本单位数量',name : 'ckBQuantity',width : 1,hide : true
				},{display : '<font color="gray">订单已出库数量</font>',name : 'ckQuantity',width : 100},
				{display : '可用库存基本单位数量</font>',name : 'kyBQuantity',width : 1 , hide:true},
				{display : '<font color="gray">可用库存数量</font>',name : 'kyQuantity',width : 100},
				{ display: '解冻基本单位数量', name: 'djjdBQuantity', width: 1 , hide:true},
                { display: '<font color="gray">本单解冻数量</font>', name: 'djjdQuantity', width: 90,
              	  render:function(item){
    					if(undefined == item.djjdQuantity || null == item.djjdQuantity || isNaN(item.djjdQuantity) ){
    			    		item.djjdQuantity = 0;
    			    		item.djjdBQuantity = 0;
    			    	}
    					
    				  return item.djjdQuantity+"";
    			  	}
                },
				{display : '<font color="gray">本单可冻结数量</font>',name : 'tmpCanFrozenQuantity',width : 100
				},
				{ display: '<font color="gray">实际冻结基本单位数量</font>', name: 'djBQuantity', width:1 , hide:true},
			    { display: '实际冻结数量<font color="red">*</font>', name: 'djQuantity', width: 80}];
 if('${billstatus}' != "" && '${billstatus}' != "0" && '${billstatus}' != "3"){
	 grid = $("#table1").ligerGrid({
	        columns: column2, 
	                      enabledEdit: true,
	                      usePager: true,pageSize:1000,pageSizeOptions:[1000],enabledSort:false, checkbox: true,
	                      url : basic_Url+"/itemlist.do?itembatch="+encodeURIComponent(batch),
	                      width: "100%", height:"98%"
	                  });
 }else{
	 grid = $("#table1").ligerGrid({
	        columns: column1, 
	                      enabledEdit: true,
	                      usePager: true,pageSize:1000,pageSizeOptions:[1000],enabledSort:false, checkbox: true,
	                      url : basic_Url+"/itemlist.do?itembatch="+encodeURIComponent(batch),
	                      width: "100%", height:"98%",
	                      toolbar: { items: [
	                                         { text: '添加', click: insert, icon: 'add' },
	                                         { line: true },
	                                         { text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
	                                         ]
	                                         }
	                  });
 }
				
				wareitemid ='';
				code ='';
			$("#pageloading").hide();
		
		});
		
		function getBatchkcQuantity(rowData){
			if(null==whcode || ""==whcode){
				$.dialog.alert("请先选择仓库");
				return ;
			}
			if (rowData.prodBatch){
	   		 $.ajax({
	     		      url: "${ctx}/yd/logic/otherOutwh/getStorageQuaitity.do",
	     		      global: false,
	     		      type: "POST",
	     		      dataType: "json",
	     		      data: ({
	     		    	maCode : rowData.prodNo,
	     				whCode : whcode,
	     				hubCode : "",
	     				status : rowData.status,
	     				batch : rowData.prodBatch
	     		      }),		     
	     		      success: function(msg){
	     		    	rowData.kyBQuantity = msg;
	     		    	rowData.auto = true;
	     	   			grid.updateCell('kyBQuantity',msg,rowData);
	     	   			if(!isNaN(rowData.prodUnitConvert)){
	 		    			rowData.kyQuantity = (rowData.kyBQuantity / rowData.prodUnitConvert);
	 		    			grid.updateCell('kyQuantity',rowData.kyQuantity,rowData);
	     	   			}
	     	   			
	     		      }
	     	   });
	  		}else{
	  			$.dialog.alert("请先选择批次");
	  		}
		}
		function getSelected(){
			var rows = grid.getCheckedRows();
			 return rows;
		}
		
		function Close(){
			CloseDialog();
		}
		function insert(){
			if('${billstatus}' != "" && '${billstatus}' != "0" && '${billstatus}' != "3"){
				return false;
			}else{
				var manager = $("#table1").ligerGetGridManager();
				manager.addRow2({
		    		materialsid: item.materialsid,
		    		prodNo: item.prodNo,
		    		prodName: item.prodName,
		    		prodSpecDesc: item.prodSpecDesc,
		    		prodBUnit : item.prodBUnit,
		    		prodUnitConvert : item.prodUnitConvert,
		    		prodUnit : item.prodUnit,
		    		status : item.status,
		    		tmpCanFrozenQuantity : item.tmpCanFrozenQuantity,
		    		prodBatch:""
				});
			}
		}
		function deleteRow()
		{ if('${billstatus}' != "" && '${billstatus}' != "0" && '${billstatus}' != "3"){
			return false;
		}else{
			grid.deleteSelectedRow();
		}
		}
		var ba=true;
		var num = 0;
		var jnum = 0;
		var kyQuantity = 0;
		var kyBQuantity = 0;
		function yz(){
			var data = grid.getData();
			if (data != null && data.length > 0) {
				jnum = 0;
				num = 0;
				for ( var i = 0; i < data.length; i++) {
					if (undefined == data[i].prodBatch
							|| "" == data[i].prodBatch) {
						$.dialog.alert("第" + (i+1) + "行的“产品批次”数据不合法");
						ba = false;
						return false;
					}
					for ( var j = 0; j < data.length; j++) {
	 					if(i != j){
	 						if(data[i].prodNo==data[j].prodNo && data[i].prodBatch==data[j].prodBatch && data[i].status==data[j].status){
	 							ba = false;
	 							$.dialog.alert("第"+(i+1)+"行与第"+(j+1)+"行存在相同的批次与等级!");
	 							return false;
	 						}
	 					}
	 				}
					if (undefined == data[i].status
							|| "" == data[i].status) {
						$.dialog.alert("第" + (i+1) + "行的“产品等级”数据不合法");
						ba = false;
						return false;
					}
					
					if (undefined == data[i].djQuantity
							|| isNaN(data[i].djQuantity)
							|| data[i].djQuantity <= 0) {
						$.dialog.alert("第" + (i+1) + "行的“实际冻结数量”数据不合法");
						ba = false;
						return false;
					}
					if(parseFloat(data[i].kyQuantity) < parseFloat(data[i].djQuantity)){
						$.dialog.alert("第" + (i+1) + "行的实际冻结数量大于可用库存数量");
						ba = false;
						return false;
					}
					if(data[i].tmpCanFrozenQuantity < data[i].djQuantity){
						$.dialog.alert("您第" + (i+1) + "的冻结数量大于冻结申请单据可冻结数量");
						ba = false;
						return false;
					}
					jnum += parseFloat(data[i].djBQuantity);
	 				num  += parseFloat(data[i].djQuantity);
	 				kyBQuantity += parseFloat(data[i].kyBQuantity);
	 				kyQuantity  += parseFloat(data[i].kyQuantity);
					if (parseFloat(num) > parseFloat(data[i].tmpCanFrozenQuantity)/* parseFloat(data[j].prodQuantity - data[j].ckQuantity) */) {
						$.dialog.alert("“本次实际冻结数量合计”大于“本单可冻结数量”");
						ba = false;
						return false;
					}
	 			}
			}
				ba=true;
		}
	</script>
	
  </head>
 <body style="padding:10px">  
 <input type="hidden" id="batchItem" name="batchItem"/>
       <div id="layout1">
       <div id="layout1">
<!--           <div class="panelBar"> -->
<!-- 	            <ul class="toolBar"> -->
<!-- 		           <li><a class="add" onclick="insert()"><span>新增</span></a></li> -->
<!-- 		       		<li><a class="delete" onclick="deleteRow()"><span>删除</span></a></li> -->
<!--                </ul> -->
<!--           </div> -->
          <div id="pageHeader" class="pageHeader">
               <div class="searchBar" id="multipleSearchDialog">
	                <table class="table" width="100%" id="searchHeader" >  
                        <tbody id="newbody">
                        </tbody>
 	                </table>
<!--                     <div class="subBar"> -->
<!-- 			             <ul> -->
<!-- 				             <li><div class="buttonActive"><div class="buttonContent"><button type="submit" id="btnSearch" onclick="insert()">添加</button></div></div></li> -->
<!--  				             <li><a class="button" onclick="clearSearch()"><span>删除</span></a></li>  -->
<!-- 			             </ul> -->
<!-- 	                </div> -->
               </div>
          </div>
      </div>
      <div position="center" id="table1">
      </div>
      <div class="clear"></div>
   </div>
    </body>
</html>